package com.bzy.blp.upms.web.controller;

import com.bzy.blp.upms.service.UserService;
import com.bzy.common.bean.Result;
import com.bzy.common.util.HttpServletUtil;
import com.bzy.common.util.LoggerUtil;
import com.bzy.common.util.local.NetworkUtil;
import org.apache.shiro.authc.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

	@Resource
	private UserService userService;

	@RequestMapping(value = {"/login"})
	public String showLoginForm(HttpServletRequest req, Model model) {
		String exceptionClassName = (String) req.getAttribute("shiroLoginFailure");
		String error = null;
		if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
			error = "用户名/密码错误";
		} else if (IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
			error = "用户名/密码错误";
		} else if ("jCaptcha.error".equals(exceptionClassName)) {
			error = "验证码错误";
		} else if (exceptionClassName != null) {
			error = "其他错误：" + exceptionClassName;
		}
		model.addAttribute("error", error);
		if (req.getParameter("forceLogout") != null) {
			model.addAttribute("error", "您已经被管理员强制退出，请重新登录");
		}
		String contentPath = req.getContextPath();

		String basePaht = NetworkUtil.getLocalAddr() + ":" + req.getServerPort() + contentPath;
		System.out.println(basePaht);
		req.setAttribute("basePath","http://"+basePaht);
		return "login";
	}


    /**
     * 登录
     * @param request
     * @param response
     * @param username
     * @param password
     * @param rememberMe
     * @return
     * @throws Exception
     */
	@RequestMapping(value = "/user/login", method = { RequestMethod.GET, RequestMethod.POST }, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public Result userLogin(HttpServletRequest request, HttpServletResponse response, String username, String password,
							boolean rememberMe) throws Exception {
		Result result = new Result();
		LoggerUtil.info(HttpServletUtil.getRequestParam(request));
		try {
			userService.userLogin(username, password, rememberMe, request.getRemoteHost());
//			userService.loginHandler();
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof UnknownAccountException) {
				result.setMessage("用户名/密码错误");
			} else if (e instanceof LockedAccountException) {
				result.setMessage("帐号锁定");
			} else if (e instanceof IncorrectCredentialsException) {
				result.setMessage("密码验证失败，失败五次账号锁定");
			} else if (e instanceof ExcessiveAttemptsException) {
				result.setMessage("失败次数过多，锁定10分钟");  // 五次密码输入错误，禁用10分钟
			} else if (e instanceof DisabledAccountException) {
				result.setMessage("账号已被禁用");
			} else {
				result.setMessage("登录错误");
			}
		}
		return result;
	}

//	@RequestMapping(value = "/register", method = RequestMethod.GET)
//	public String showRegisterForm(HttpServletRequest req, Model model) {
//		return "register";
//	}
//
//	@RequestMapping(value = "/register", method = RequestMethod.POST)
//	@ResponseBody
//	public Map<String, Object> doRegister(String vcode, UUser entity) {
//		Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
//		resultMap.put("message", "注册成功！");
//		resultMap.put("status", 200);
//		return resultMap;
//	}

}
